//
//  CustomPullToRefresh.m
//  PullToRefreshDemo
//
//  Created by John Wu on 3/22/12.
//  Copyright (c) 2012 TFM. All rights reserved.
//

#import "CustomPullToRefresh.h"

@interface CustomPullToRefresh()
@property (strong, nonatomic) UIScrollView *aScrollView;
@end

@implementation CustomPullToRefresh

- (id) initWithScrollView:(UIScrollView *)scrollView delegate:(id<CustomPullToRefreshDelegate>)delegate {
    self = [super init];
    if (self) {
        self.enablePullToBottom = YES;
        _delegate = delegate;
        self.aScrollView = scrollView;
        [self.aScrollView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

        _ptrc = [[MSPullToRefreshController alloc] initWithScrollView:self.aScrollView delegate:self];
        
        //绘制顶部
        _pullTop = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading-1.png"]];
        _pullTop.frame = CGRectMake(0, -self.aScrollView.frame.size.height, self.aScrollView.frame.size.width, scrollView.frame.size.height);
        [scrollView addSubview:_pullTop];
        //顶部箭头
        _arrowTop = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"big_arrow.png"]];
        _arrowTop.frame = CGRectMake(floorf((_pullTop.frame.size.width-_arrowTop.frame.size.width)/2), _pullTop.frame.size.height - _arrowTop.frame.size.height - 10 , _arrowTop.frame.size.width, _arrowTop.frame.size.height);
        [_pullTop addSubview:_arrowTop];
        //顶部菊花
        _gearTop = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _gearTop.frame = _arrowTop.frame;
        _gearTop.hidden = YES;
        [_pullTop addSubview:_gearTop];
        //绘制底部
        _pullBot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading-1.png"]];
        _pullBot.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        _pullBot.frame = CGRectMake(0, self.aScrollView.frame.size.height, self.aScrollView.frame.size.width, scrollView.frame.size.height);
        [scrollView addSubview:_pullBot];
        //底部箭头
        _arrowBot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"big_arrow.png"]];
        _arrowBot.frame = CGRectMake(floorf((_pullBot.frame.size.width-_arrowBot.frame.size.width)/2), 10 , _arrowBot.frame.size.width, _arrowBot.frame.size.height);
        _arrowBot.transform  = CGAffineTransformMakeRotation(M_PI);
        [_pullBot addSubview:_arrowBot];
        //底部菊花
        _gearBot = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _gearBot.frame = _arrowBot.frame;
        _gearBot.hidden = YES;
        [_pullBot addSubview:_gearBot];
    }
    return self;
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@",NSStringFromCGSize(self.aScrollView.contentSize));
    CGFloat contentSizeArea = self.aScrollView.contentSize.width*self.aScrollView.contentSize.height;
    CGFloat frameArea = self.aScrollView.frame.size.width*self.aScrollView.frame.size.height;
    CGSize adjustedContentSize = contentSizeArea < frameArea ? self.aScrollView.frame.size : self.aScrollView.contentSize;
    _pullBot.frame = CGRectMake(0, adjustedContentSize.height, self.aScrollView.frame.size.width, self.aScrollView.frame.size.height);
}

//- (void) dealloc {
//    [self.aScrollView removeObserver:self forKeyPath:@"contentSize"];
//}

- (void) endRefresh {
    [_ptrc finishRefreshingDirection:MSRefreshDirectionTop animated:YES];
    [_ptrc finishRefreshingDirection:MSRefreshDirectionBottom animated:YES];
//    [_pullTop stopAnimating];
//    [_pullBot stopAnimating];
    [_gearTop stopAnimating];
    _gearTop.hidden = YES;
    [_gearBot stopAnimating];
    _gearBot.hidden = YES;
    _arrowBot.hidden = NO;
    _arrowBot.transform  = CGAffineTransformMakeRotation(M_PI);
    _arrowTop.hidden = NO;
    _arrowTop.transform = CGAffineTransformIdentity;
}

- (void) startRefresh {
    [_ptrc startRefreshingDirection:MSRefreshDirectionTop];
}

-(void)setEnablePullToBottom:(Boolean)enablePullToBottom
{
    _pullBot.hidden = !enablePullToBottom;
    _enablePullToBottom = enablePullToBottom;
}

#pragma mark - MSPullToRefreshDelegate Methods
//控制可以拉动的方向
- (BOOL) pullToRefreshController:(MSPullToRefreshController *)controller canRefreshInDirection:(MSRefreshDirection)direction {
    if (self.enablePullToBottom) {
        return direction == MSRefreshDirectionTop || direction == MSRefreshDirectionBottom;
    } else {
        return direction == MSRefreshDirectionTop;
    }
    
}

- (CGFloat) pullToRefreshController:(MSPullToRefreshController *)controller refreshingInsetForDirection:(MSRefreshDirection)direction {
    return 30;
}

- (CGFloat) pullToRefreshController:(MSPullToRefreshController *)controller refreshableInsetForDirection:(MSRefreshDirection)direction {
    return 30;
}

//拉动到制定高度，但是还没放开
- (void) pullToRefreshController:(MSPullToRefreshController *)controller canEngageRefreshDirection:(MSRefreshDirection)direction {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    _arrowTop.transform = CGAffineTransformMakeRotation(M_PI);
    _arrowBot.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

//没拉动到指定高度
- (void) pullToRefreshController:(MSPullToRefreshController *)controller didDisengageRefreshDirection:(MSRefreshDirection)direction {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    _arrowTop.transform = CGAffineTransformIdentity;
    _arrowBot.transform  = CGAffineTransformMakeRotation(M_PI);
    [UIView commitAnimations];
}

//拉动到制定高度，放开
- (void) pullToRefreshController:(MSPullToRefreshController *)controller didEngageRefreshDirection:(MSRefreshDirection)direction {
    _arrowTop.hidden = YES;
    _arrowBot.hidden = YES;
//    [_rainbowTop startAnimating];
//    [_rainbowBot startAnimating];
    _gearTop.hidden = NO;
    [_gearTop startAnimating];
    _gearBot.hidden = NO;
    [_gearBot startAnimating];
    [_delegate customPullToRefreshShouldRefresh:self withDirection:direction];
}

@end
